package com.lu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.model.entity.SysResources;
import com.lu.model.request.ResourcesQueryRequest;
import com.lu.model.response.Result;
import com.lu.service.SysResourcesService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author zhanglu
 * @since 2022-07-26
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys-resources")
public class SysResourcesController {
  
  private final SysResourcesService resourcesService;

  @PreAuthorize("hasAuthority('/sys-resources/add')")
  @PostMapping(value = "/add")
  public Result<Long> addResources(@RequestBody SysResources resources){
    return Result.success(resourcesService.addResources(resources));
  }

  @PreAuthorize("hasAuthority('/sys-resources/update')")
  @PostMapping(value = "/update")
  public Result<SysResources> updateResources(@RequestBody SysResources resources){
    return Result.success(resourcesService.updateResources(resources));
  }

  @PreAuthorize("hasAuthority('/sys-resources/update-status')")
  @PostMapping(value = "/update-status/{id}/{status}")
  public Result<Integer> updateStatus(@PathVariable Long id, @PathVariable Integer status){
    return Result.success(resourcesService.updateStatus(id, status));
  }

  @PreAuthorize("hasAuthority('/sys-resources/delete')")
  @PostMapping(value = "/delete/{id}")
  public Result<String> deleteResources(@PathVariable Long id){
    return Result.success(resourcesService.deleteResources(id));
  }

  @PreAuthorize("hasAuthority('/sys-resources/page-list')")
  @PostMapping(value = "/page-list")
  public Result<Page<SysResources>> pageList(@RequestBody ResourcesQueryRequest request){
    return Result.success(resourcesService.pageList(request));
  }

  @PreAuthorize("hasAuthority('/sys-resources/sort')")
  @PostMapping("/sort")
  public Result<List<SysResources>> sort(@RequestBody List<Long> ids){
    return Result.success(resourcesService.sort(ids));
  }

  /**
   * 用户详情
   * @param id
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-resources/get')")
  @GetMapping(value = "/get/{id}")
  public Result<SysResources> getById(@PathVariable Long id){
    return Result.success(resourcesService.getById(id));
  }

  @PreAuthorize("hasAuthority('/sys-resources/tree-nodes')")
  @GetMapping(value = "/tree-nodes")
  public Result<List<SysResources.TreeNodes>> treeNodes(@RequestParam(required = false) Long pid, @RequestParam(required = false) Integer status){
    return Result.success(resourcesService.getListByPid(pid, status));
  }

}
